package com.javatribe.scoreSystem.controller;

import com.javatribe.scoreSystem.cache.ResultCache;
import com.javatribe.scoreSystem.entity.Result;
import com.javatribe.scoreSystem.entity.Score;
import com.javatribe.scoreSystem.entity.Team;
import com.javatribe.scoreSystem.service.JudgeService;
import com.javatribe.scoreSystem.service.ResultService;
import com.javatribe.scoreSystem.utils.JSONUtil;
import com.javatribe.scoreSystem.vo.BackTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author 大雨两点
 * @version 1.0
 * @date 2020/7/17 15:51
 */

@Controller
@RequestMapping("/judge")
public class JudgeController {
    @Autowired
    private JudgeService judgeService;
    @Autowired
    private ResultService resultService;

    @ResponseBody
    @PostMapping("/getNoScoreTeam")
    public List<Team> getNoScoreTeam(@RequestBody Map<String, Integer> map) {
        List<Team> teams = judgeService.noScoreTeam(map);
//        String json = JSONUtil.toJSON(teams);
        return teams;
    }

    @ResponseBody
    @PostMapping("/getHasScoreTeam")
    public List<Result> getHasScoreTeam(@RequestBody Map<String, Integer> map) {
        List<Result> results = judgeService.hasScoreTeam(map);
//        String json = JSONUtil.toJSON(results);
        return results;
    }

    @ResponseBody
    @PostMapping("/insertScore")
    public BackTo insertScore(@RequestBody Score score) {
        int result = judgeService.insertScore(score);
        if (1 == result) {
            ResultCache.resultCache = resultService.getResultSet();
            return new BackTo("评分成功！", 200);
        } else {
            return new BackTo("评分失败！请尝试刷新", 5000);
        }
    }

    @ResponseBody
    @PostMapping("/updateScore")
    public BackTo updateScore(@RequestBody Score score) {
        int result = judgeService.updateScore(score);
        if (1 == result) {
            ResultCache.resultCache = resultService.getResultSet();
            return new BackTo("已修改评分！", 200);
        } else {
            return new BackTo("修改失败！请尝试刷新", 5000);
        }
    }

    @ResponseBody
    @PostMapping("/updateStatus")
    public BackTo updateStatus(@RequestBody Team team) {
        int result = judgeService.updateStatus(team);
        if (1 == result) {
            return new BackTo("该队伍已改为上场状态！", 200);
        } else {
            return new BackTo("操作失败！请刷新重试", 5000);
        }
    }
}
